package com.springboot.test.web.app;

import com.springboot.test.common.response.Response;
import com.springboot.test.common.enums.StaticKey;
import com.springboot.test.service.param.*;
import com.springboot.test.service.UserService;
import com.springboot.test.service.result.UserDetailResult;
import com.springboot.test.service.result.UserInfoResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 测试类
 */
@RestController
@RequestMapping("app/user")
public class UserController {
    @Resource
    private UserService userService;

    @GetMapping("result")
    public Response<String> getResult(@RequestAttribute(StaticKey.USER_NAME) String userCode) {
        return Response.success(userService.test(userCode));
    }

    /**
     * 添加用户
     * @param param
     * @return
     */
    @PostMapping("add")
    public Response<Void> insert(@RequestBody AddUserParam param) {
        return Response.success(userService.insert(param));
    }

    /**
     * 用户登录
     * @param param
     * @return
     */
    @PostMapping("login")
    public Response<String> login(@RequestBody LoginParam param) {
        return Response.success(userService.login(param));
    }

    /**
     * 查询登录用户的基本信息
     * @param userCode
     * @return
     */
    @GetMapping("info")
    public Response<UserInfoResult> info(@RequestAttribute(StaticKey.USER_CODE) String userCode){
        return Response.success(userService.info(userCode));
    }

    /**
     * 获取用户详情
     * @param userCode
     * @return
     */
    @GetMapping("detail")
    public Response<UserDetailResult> detailByCode(@RequestAttribute(StaticKey.USER_CODE) String userCode){
        return Response.success(userService.detail(userCode));
    }

    /**
     * 更新用户详情
     * @param param
     * @param userCode
     * @return
     */
    @PostMapping("update_detail")
    public Response<Boolean> updateDetail(@RequestBody UpdateDetailParam param,@RequestAttribute(StaticKey.USER_CODE) String userCode){
            return Response.success(userService.updateDetail(param,userCode));
    }


    @PostMapping("update_password")
    public Response<Boolean> updatePassword(@RequestBody UpdatePasswordParam param, @RequestAttribute(StaticKey.USER_CODE) String userCode,HttpServletRequest request){
        String token = request.getHeader("token");
        return Response.success(userService.updatePassword(param,userCode,token));
    }

    @GetMapping("log_out")
    public Response<Boolean> logout(HttpServletRequest request){
        String token = request.getHeader("token");
        return Response.success(userService.logout(token));
    }

}
